package com.weiwei.diancan.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.weiwei.diancan.Contant.Config;
import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.R;
import com.weiwei.diancan.base.BaseActivity;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.bean.WaterInfo;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.http.RetrofitFactory;
import com.weiwei.diancan.utils.LoggerUtils;
import com.weiwei.diancan.utils.ViewGetTextUtils;
import com.weiwei.diancan.view.EmptyLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.views.ToastUtil;

public class BangdingActivity extends BaseActivity {

    private EditText et_login_num;
    private EditText et_login_pass;
    private Button bt_login;
    private String login_num;
    private String login_pass;
    private String text_num;
    private String text_pass;

    @Override
    public void getData() {

    }

    @Override
    public void setListeners() {
        bt_login.setOnClickListener(v->login());
        et_login_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                text_num = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                notNull();
            }
        });

        et_login_pass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                text_pass = s.toString();

            }

            @Override
            public void afterTextChanged(Editable s) {
                notNull();
            }
        });
    }

    @Override
    public void initView() {
        et_login_num = (EditText)findViewById(R.id.et_login_num);
        et_login_pass = (EditText)findViewById(R.id.et_login_pass);
        bt_login = (Button)findViewById(R.id.bt_login);
    }

    @Override
    public void initTitle() {
        titler.setText("御膳房");
        iv_left.setVisibility(View.VISIBLE);
        iv_left.setText("返回");
        iv_left.setOnClickListener(v->finish());
    }

    @Override
    public void setLayoutView() {
        setContentView(R.layout.activity_login);
    }

    /**
     * 登录
     */
    private void login(){
        if(TextUtils.isEmpty(ViewGetTextUtils.getEditText(et_login_num,"工号不能为空")))return;
        else login_num = ViewGetTextUtils.getEditText(et_login_num,"工号不能为空");

        if(TextUtils.isEmpty(ViewGetTextUtils.getEditText(et_login_pass,"密码不能为空")))return;
        else login_pass = ViewGetTextUtils.getEditText(et_login_pass,"密码不能为空");

        Map<String,String> map = new HashMap<>();
        map.put(ConfigMethod.shop_token,CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.water_user_name,login_num);
        map.put(ConfigMethod.water_user_pwd,login_pass);
        RetrofitFactory.getIntance().waiter_login(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WaterInfo>() {
                    @Override
                    public void onStart() {
                        progressDialog.setMessage("绑定中...");
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.cancel();
                    }

                    @Override
                    public void onNext(WaterInfo waterInfo) {
                        if(waterInfo.getIs_success().equals(ConfigMethod.Success)){
                            ToastUtil.showToast(BangdingActivity.this,waterInfo.getReason());
                            CacheConfig.getInst().putWaterToken(waterInfo.getWaiter_info().getWaiter_token(),BangdingActivity.this);
                            CacheConfig.getInst().putWaterInfo(waterInfo.getWaiter_info());
                            CacheConfig.getInst().putWator_per(waterInfo.getWaiter_info().getWaiter_jurisdiction(),BangdingActivity.this);
                            progressDialog.setMessage("数据更新中...");
                            onRefreshData();
                        }else{
                            ToastUtil.showToast(BangdingActivity.this,waterInfo.getReason());
                            progressDialog.cancel();
                        }

                    }
                });
    }

    /**
     *获取数据
     */
    private void onRefreshData() {

        if (TextUtils.isEmpty(CacheConfig.getInst().getToken(this))) {
            ToastUtil.showToast(this, "没有登录！");
            return;
        }

        if (TextUtils.isEmpty(CacheConfig.getInst().getWaterToken(this))) {
            ToastUtil.showToast(this, "请绑定工号！");
            return;
        }


        Map<String, String> map = new HashMap<>();
        map.put(ConfigMethod.shop_token, CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.waiter_token, CacheConfig.getInst().getWaterToken(this));
        RetrofitFactory.getIntance().refreshData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.cancel();
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if(loginBean.getIs_success().equals(ConfigMethod.Success)) {
                            loginBean.setShop_token(CacheConfig.getInst().getToken(BangdingActivity.this));
                            EventBus.getDefault().post(Config.BIND_SUCCESS);
                            progressDialog.cancel();
                            openActivity(Main1Activity.class,null);
                            BangdingActivity.this.finish();
                        }else{
                            progressDialog.cancel();
                            ToastUtil.showToast(BangdingActivity.this,loginBean.getReason());
                        }

                    }
                });
    }

    private void notNull(){
        if(!TextUtils.isEmpty(text_num)&&!TextUtils.isEmpty(text_pass)) {
            bt_login.setBackgroundResource(R.color.home_recommend_goods_tv_sales_price);
        }else{
            bt_login.setBackgroundResource(R.color.home_recommend_goods_tv_sales_price_train);
        }
    }
}
